package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

//新增套餐
    @PostMapping
   public R<String>save(@RequestBody SetmealDto setmealDto)
   {
       setmealService.saveWithDish(setmealDto);
       return R.success("新增套餐成功");
   }


    //套餐分页查询
   /* @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name)
    {
        Page<Setmeal>pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal>lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,lqw);//原始数据

        Page<SetmealDto>list=new Page<>();
        BeanUtils.copyProperties(pageInfo,list,"records");//对拷去掉数据部分

        //对数据处理
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto>dtoList=new ArrayList<>();

        records.forEach(setmeal -> {
            SetmealDto dto=new SetmealDto();

            BeanUtils.copyProperties(setmeal,dto);
            Category byId = categoryService.getById(setmeal.getCategoryId());
            dto.setCategoryName(byId.getName());
            dtoList.add(dto);
        });
        list.setRecords(dtoList);//将数据添加回
        return R.success(list);

    }*/
    //分页查询套餐
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name)
    {
        Page<Setmeal>pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal>lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,lqw);

        Page<SetmealDto>list=new Page<>();
        BeanUtils.copyProperties(pageInfo,list,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> dtoList=new ArrayList<>();
       records.forEach(setmeal -> {
           SetmealDto dto=new SetmealDto();
           BeanUtils.copyProperties(setmeal,dto);

           Long categoryId = setmeal.getCategoryId();
           Category byId = categoryService.getById(categoryId);
           dto.setCategoryName(byId.getName());
           dtoList.add(dto);

       });
        list.setRecords(dtoList);
        return R.success(list);


    }




    //删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long>ids)
    {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    //根据分类ID查询套餐信息（手机端
    //
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
